package com.unitedats.common.util;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.unitedats.common.Entity.SysUserLogin;
import com.unitedats.common.bo.Message;

public class WebUtil {

	public static SysUserLogin getSessionUser(HttpServletRequest request) {
		HttpSession s = request.getSession(false);
		SysUserLogin sUser = null;
		if (s != null)
			sUser = (SysUserLogin) s.getAttribute("user");
		return sUser;
	}

	public static String getSessionMessage(HttpServletRequest request) {
		Message msg = null;
		HttpSession s = request.getSession(false);
		if (s != null) {
			msg = (Message) s.getAttribute("msg");
			s.removeAttribute("msg");
		}
		if (msg != null) {
			if (msg.getType() == Message.MESSAGE_TYPE.ERROR)
				return "<h3 class='error_msg'>" + msg.getText() + "</h3>";
			else if (msg.getType() == Message.MESSAGE_TYPE.WARNING)
				return "<h3 class='warning_msg'>" + msg.getText() + "</h3>";
			else if (msg.getType() == Message.MESSAGE_TYPE.SUCCESS)
				return "<h3 class='success_msg'>" + msg.getText() + "</h3>";
		}
		return "";
	}

	public static Integer getParamValueAsInteger(HttpServletRequest request, String paramName, Integer defaultValue) {
		try {
			return Integer.parseInt(request.getParameter(paramName));
		} catch (Exception e) {
			return defaultValue;
		}
	}

	public static Float getParamValueAsFloat(HttpServletRequest request, String paramName, Float defaultValue) {
		try {
			return Float.parseFloat(request.getParameter(paramName));
		} catch (Exception e) {
			return defaultValue;
		}
	}

	public static String getParamValue(HttpServletRequest request, String paramName, String defaultValue) {
		String inputStr = request.getParameter(paramName);
		if (inputStr != null && !inputStr.trim().isEmpty())
			// /return StringEscapeUtils.escapeSql(inputStr);
			return inputStr;
		// return StringEscapeUtils.escapeSql(defaultValue);
		return defaultValue;
	}

	public static String[] getParamValues(HttpServletRequest request, String paramName) {
		// if (request.getParameterValues(paramName) != null)
		return request.getParameterValues(paramName);
		// return new String[] {};
	}

	public static Date getParamValueAsDate(HttpServletRequest request, String paramName, Date defaultValue) {
		try {
			Date d = DateUtil.convertToDate(request.getParameter(paramName));
			return (d != null) ? d : defaultValue;
		} catch (Exception e) {
			return defaultValue;
		}
	}

	public static String decodeQueryStringParameter(HttpServletRequest request, String paramName) {

		/*
		 * try { return URLDecoder.decode(request.getParameter(paramName),
		 * "UTF-8"); } catch (UnsupportedEncodingException e) { Common.log(e);
		 * return null; }
		 */

		try {
			String value = new String(request.getParameter(paramName).getBytes("iso-8859-1"), "utf-8");
			return (value != null) ? value : "";
		} catch (UnsupportedEncodingException e) {
			Common.log(e);
			return "";
		}

	}
}